package com.lmm.videopager_java;

import android.media.MediaPlayer;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;

import com.lmm.videopager_java.databinding.FragmentPlayerBinding;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PlayerFragment extends Fragment {

    private MediaPlayer mediaPlayer = new MediaPlayer();
    private FragmentPlayerBinding binding;
    private String url;

    //简单代替协程的办法，只适用于少数场景
    private final ExecutorService executorService = Executors.newCachedThreadPool();


    public PlayerFragment(String url) {
        this.url = url;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentPlayerBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        mediaPlayer.release();
        try {
            mediaPlayer.setOnPreparedListener(mp -> {
                binding.progressBar2.setMax(mediaPlayer.getDuration());
                mp.seekTo(1);
//                mediaPlayer.start();
                binding.progressBar.setVisibility(View.INVISIBLE);
            });
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepareAsync();
            binding.progressBar.setVisibility(View.VISIBLE);

        } catch (IOException e) {
            e.printStackTrace();
        }


        binding.surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {

            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
                mediaPlayer.setDisplay(holder);
                mediaPlayer.setScreenOnWhilePlaying(true);
            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

            }
        });


        executorService.submit(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        binding.progressBar2.setProgress(mediaPlayer.getCurrentPosition());
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        mediaPlayer.start();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                while (!mediaPlayer.isPlaying()) {
                    try {
                        mediaPlayer.start();
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        //上下滑动的时候，让视频从头开始播放，类似抖音的效果
        mediaPlayer.seekTo(1);
        mediaPlayer.pause();
//        executorService.shutdown();
    }

    public void setUrl(String url) {
        this.url = url;
    }
}